package com.huluobo.a2208parkdemo.news

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.RadioButton
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2208parkdemo.R
import com.huluobo.a2208parkdemo.adapter.NewsAdapter
import com.huluobo.a2208parkdemo.databinding.ActivityNewsBinding
import com.huluobo.base.bean.NewsBean
import com.huluobo.base.bean.NewsBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.parse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding, NewsViewModel>() {
    //    private lateinit var newsAdapter: NewsAdapter
    private lateinit var newsPagingAdapter: NewsPagingAdapter
    private var page: Int = 0
    override fun initData() {
//        val map = mutableMapOf<String, Int>()
//        map[Const.PARAM_PAGE] = page
//        val json = Gson().toJson(map)
//        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
//
//        vm.getNews(body)
//
//        vm.getNewsSuccess.observe(this) {
//            val list = Gson().fromJson(it, NewsBean::class.java)
//            newsAdapter.data.clear()
//            newsAdapter.data.addAll(list)
//            newsAdapter.notifyDataSetChanged()
//        }

        vm.addNewsSuccess.observe(this) {
            ToastUtils.showLong("添加新闻成功")
//            vm.getNews(body)
        }
    }

    override fun initView() {
        binding.newsHeader.headerTitle.text = "新闻"
        binding.newsHeader.headerBack.setOnClickListener { finish() }
        binding.newsPageTv.text = "第${page + 1}页"
        binding.newsHeader.headerAdd.visibility = View.VISIBLE
        binding.newsHeader.headerAdd.setOnClickListener {
            val dialogView = LayoutInflater.from(this).inflate(R.layout.view_add_news, null)
            AlertDialog.Builder(this)
                .setView(dialogView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定") { dialog, which ->
                    val newsTitle = dialogView.findViewById<EditText>(R.id.add_news_title_et).text.toString()
                    val newsValue = dialogView.findViewById<EditText>(R.id.add_news_value_et).text.toString()
                    val newsTime = getTime()
                    var tagId = -1
                    when {
                        dialogView.findViewById<RadioButton>(R.id.add_news_wuye).isChecked -> tagId = 0
                        dialogView.findViewById<RadioButton>(R.id.add_news_fun).isChecked -> tagId = 1
                        dialogView.findViewById<RadioButton>(R.id.add_news_sport).isChecked -> tagId = 2
                        dialogView.findViewById<RadioButton>(R.id.add_news_society).isChecked -> tagId = 3
                        dialogView.findViewById<RadioButton>(R.id.add_news_morning).isChecked -> tagId = 4
                    }

                    if (newsTitle.isNotEmpty() && newsValue.isNotEmpty() && newsTime.isNotEmpty() && tagId != -1) {
                        val map = mutableMapOf<String, Any>()
                        map[Const.PARAM_NEWS_TITLE] = newsTitle
                        map[Const.PARAM_NEWS_VALUE] = newsValue
                        map[Const.PARAM_NEWS_TIME] = newsTime
                        map[Const.PARAM_NEWS_TAG_ID] = tagId

                        val json = Gson().toJson(map)
                        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                        vm.addNews(body)
                    }
                }
                .show()

        }

        binding.newsRv.layoutManager = LinearLayoutManager(this)
        newsPagingAdapter = NewsPagingAdapter()
        binding.newsRv.adapter = newsPagingAdapter

        lifecycleScope.launch {
            vm.getNewsPagingData().collect {
                newsPagingAdapter.submitData(it)
            }
        }

        binding.preBtn.setOnClickListener {
            if (page == 0) {
                ToastUtils.showLong("已经是第一页了")
            } else {
                page--
                binding.newsPageTv.text = "第${page + 1}页"
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_PAGE] = page
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)

                vm.getNews(body)
            }
        }

        binding.nextBtn.setOnClickListener {
            page++
            binding.newsPageTv.text = "第${page + 1}页"
            val map = mutableMapOf<String, Int>()
            map[Const.PARAM_PAGE] = page
            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)

            vm.getNews(body)
        }
    }
}